package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import util.resources.ResourceManager;

/**
 * Class contained in Turtles used to determine how drawing of turtles occurs.
 * This class defines default behavior, which can be overridden in child
 * classes.
 * 
 * @author Nathan Klug
 * @author Anne Weng
 * 
 */

public class Pen
{
    private static ResourceManager defaults = ResourceManager.getInstance();

    private Color myLineColor;
    private boolean isPenDown;
    private Float myLineWidth;

    /*
     * Default values
     */
    private static final Color getDefaultColor()
    {
        return defaults.getColor("Pen_color");
    }

    private static final boolean defaultPenState = defaults
            .getBoolean("Pen_down");
    private static final Float defaultLineWidth = defaults
            .getFloat("Line_width");

    public Pen()
    {
        this(getDefaultColor());
    }

    public Pen(Color lineColor)
    {
        myLineColor = lineColor;
        isPenDown = defaultPenState;
        myLineWidth = defaultLineWidth;
    }

    public Pen(Color lineColor, boolean penState)
    {
        myLineColor = lineColor;
        isPenDown = penState;
        myLineWidth = defaultLineWidth;
    }

    public Pen(Color lineColor, boolean penState, Float lineWidth)
    {
        myLineColor = lineColor;
        isPenDown = penState;
        myLineWidth = lineWidth;
    }

    /**
     * Draws a line from a previous point to the turtle's current location.
     * 
     * @param surface
     * @param prevX
     * @param prevY
     * @param canvasSize
     */
    public void drawShape(Pixmap pixmap, Shape s)
    {
        if (isPenDown())
            pixmap.setPropertiesAndDraw(this, s, myLineWidth);
    }

    /**
     * Sets a new property for the Turtle's pen. Currently only does color.
     * TODO: more properties
     * 
     * @param Graphics
     */
    public Graphics setDrawProperties(Graphics g)
    {
        g.setColor(myLineColor);
        return g;
    }

    /**
     * Sets a new color for the Turtle's pen.
     * 
     * @param color
     */
    public void setColor(Color color)
    {
        myLineColor = color;
    }

    /**
     * Boolean for the pen's status up/down.
     * 
     * @return boolean
     */
    public boolean isPenDown()
    {
        return isPenDown;
    }

    /**
     * Gets current line width.
     */
    public Float getLineWidth()
    {
        return myLineWidth;
    }

    /**
     * Sets isPenDown to some given boolean
     * 
     * @param penIsDown
     */
    public void setPenDown(boolean penState)
    {
        isPenDown = penState;
    }

    /**
     * Puts the pen up.
     */
    public void penUp()
    {
        isPenDown = false;
    }

    /**
     * Puts the pen down.
     */
    public void penDown()
    {
        isPenDown = true;
    }

    public Pen copy()
    {
        Pen newPen = new Pen();

        /*
         * Construct a new Color object.
         */
        int r, g, b;
        r = myLineColor.getRed();
        g = myLineColor.getGreen();
        b = myLineColor.getBlue();
        Color newColor = new Color(r, g, b);

        newPen.setColor(newColor);
        if (isPenDown())
        {
            return newPen;
        }
        else
        {
            newPen.penUp();
            return newPen;
        }
    }

}
